package com.sskj.aikeeper.hangqing.activity;

import android.arch.lifecycle.Observer;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.sskj.aikeeper.R;
import com.sskj.aikeeper.component.DaggerUserDataComponent;
import com.sskj.aikeeper.presenter.ChangePayPresenter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.HideUtil;

import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/06/25
 */
public class ChangePayActivity extends BaseActivity<ChangePayPresenter> {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.tv_getcode)
    TextView tvGetcode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_old_pwd)
    EditText etOldPwd;
    @BindView(R.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;


    @Override
    public int getLayoutId() {
        return R.layout.app_activity_change_pay;
    }

    @Override
    public ChangePayPresenter getPresenter() {
        return new ChangePayPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        tvTitle.setText(App.INSTANCE.getString(R.string.app_changePayActivity1));
    }

    @Override
    public void initData() {
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                tvMobile.setText(String.format(App.INSTANCE.getString(R.string.app_changePayActivity2), HideUtil.getPhoneHide(userData.getMobile())));
            } else {
                userData = null;
            }
        });
        ClickUtil.click(300, tvGetcode, () -> {
            if (userData == null) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_changePayActivity3));
                return;
            }
            mPresenter.getRegisterCode(userData.getMobile());
        });
        ClickUtil.click(300, btnConfirm, () -> {
            if (userData == null) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_changePayActivity3));
                return;
            }
            if (isEmpty(getText(etCode))) {
                toastHint(etCode);
                return;
            }
            if (isEmpty(getText(etOldPwd))) {
                toastHint(etOldPwd);
                return;
            }
            if (!CheckUtil.isPayPwd(getText(etOldPwd))) {
                toastHint(etOldPwd);
                return;
            }
            if (isEmpty(getText(etNewPwd))) {
                toastHint(etNewPwd);
                return;
            }
            if (!getText(etOldPwd).equals(getText(etNewPwd))) {
                ToastUtils.show(App.INSTANCE.getString(R.string.app_changePayActivity4));
                return;
            }
            mPresenter.changePayPwd(userData.getMobile(), getText(etOldPwd), getText(etNewPwd), getText(etCode));
        });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ChangePayActivity.class);
        context.startActivity(intent);
    }

    public void sendOK() {
        startTimeDown(tvGetcode);
    }
}
